<?php
/**
 * 积分礼品
 *
 * @copyright  Copyright (c) 2007-2013 ShopNC Inc. (http://www.shopnc.net)
 * @license    http://www.shopnc.net
 * @link       http://www.shopnc.net
 * @since      File available since Release v1.1
 */
defined ( 'InShopNC' ) or exit ( 'Access Invalid!' );
class pointprodControl extends mobileControl {
	private $templatestate_arr;
	public function __construct() {
		parent::__construct ();
		// 读取语言包
		Language::read ( 'home_pointprod,home_voucher' );
		// 判断系统是否开启积分和积分中心功能
		if (C ( 'points_isuse' ) != 1 || C ( 'pointshop_isuse' ) != 1) {
			mobileJump ( Language::get ( 'pointshop_unavailable' ), 'index.php' );
		}
		// 根据op判断积分兑换功能是否开启
		if (in_array ( $_GET ['op'], array (
				'plist',
				'pinfo' 
		) ) && C ( 'pointprod_isuse' ) != 1) {
			mobileJump ( Language::get ( 'pointprod_unavailable' ), 'index.php' );
		}
		if ($_SESSION ['is_login'] == '1') {
			$model = Model ();
			if (C ( 'pointprod_isuse' ) == 1) {
				// 已选择兑换商品数
				$pcartnum = $model->table ( 'points_cart' )->where ( array (
						'pmember_id' => $_SESSION ['member_id'] 
				) )->count ();
				Tpl::output ( 'pcartnum', $pcartnum );
			}
			// 查询会员信息
			$member_info = $model->table ( 'member' )->field ( 'member_points,member_avatar,member_provinceid' )->where ( array (
					'member_id' => $_SESSION ['member_id'] 
			) )->find ();
			Tpl::output ( 'member_info', $member_info );
		}
	}
	
	/**
	 * 积分商品列表
	 */
	public function indexOp() {
		$where = '';
        $search_name = trim($_GET['keyword']);
		if(!empty($search_name)){
			$where .= " AND pgoods_name like '%$search_name%'";
		}
        $pageSize = 20;
        $curPage = $_GET['curpage'];
        $sql = "SELECT * FROM #__points_goods WHERE pgoods_show = '1' AND pgoods_state = '0' $where ORDER BY pgoods_sort asc,pgoods_id desc";
        $rs = db::PageQuery($sql, $curPage, $pageSize);
        $pointprod_list = $rs->fetchAll();
        $pointprod_list=$this->Timg($pointprod_list);
        $page = new Page();
        $page->setEachNum($pageSize);
        $page->setNowPage($curPage);
        $page->setTotalNum($rs->pageRows());
        $page->setStyle('admin');
				
		$this->_assignMobileData($page);
        Tpl::output('pointprod_list', $pointprod_list);
        Tpl::output('html_title', '积分兑换商品中心——D1美财网');
        Tpl::showpage('pointprod_list');
	}
	/**
	 * 积分礼品详细
	 */
	public function pinfoOp() {
		$pid = intval ( $_GET ['id'] );
		if (! $pid) {
			mobileJump ( Language::get ( 'pointprod_parameter_error' ), 'index.php?act=pointprod');
		}
		$model = new pointprodModel ();
		$prodinfo = $model->getPointProdInfoNew ( array (
				'pgoods_id' => $pid 
		) );
		if (! is_array ( $prodinfo ) || count ( $prodinfo ) <= 0) {
			mobileJump ( Language::get ( 'pointprod_record_error' ), 'index.php?act=pointprod');
		}
		// 兑换按钮的可用状态
		if ($prodinfo ['pgoods_islimittime'] == 1 && $prodinfo ['ex_state'] == 'going') {
			$timediff = intval ( $prodinfo ['pgoods_endtime'] ) - time ();
			$prodinfo ['timediff'] ['diff_day'] = intval ( $timediff / 86400 );
			$prodinfo ['timediff'] ['diff_hour'] = intval ( $timediff % 86400 / 3600 );
			$prodinfo ['timediff'] ['diff_mins'] = intval ( $timediff % 86400 % 3600 / 60 );
			$prodinfo ['timediff'] ['diff_secs'] = intval ( $timediff % 86400 % 3600 % 60 );
		}
		// 更新礼品浏览次数
		$model->table ( 'points_goods' )->where ( array (
				'pgoods_id' => $pid 
		) )->update ( array (
				'pgoods_view' => array (
						'exp',
						'pgoods_view+1' 
				) 
		) );
		
		Tpl::output ( 'prodinfo', $prodinfo );
		
		$seo_param = array ();
		$seo_param ['name'] = $prodinfo ['pgoods_name'];
		$seo_param ['key'] = $prodinfo ['pgoods_keywords'];
		$seo_param ['description'] = $prodinfo ['pgoods_description'];
		Model ( 'seo' )->type ( 'point_content' )->param ( $seo_param )->show ();
		Tpl::showpage ( 'pointprod_info' );
	}

    /*
     * 图片路径处理
     * */
    private function Timg($list){
        if (is_array($list) && count($list)>0){
            foreach ($list as $k=>$v){
                $v['pgoods_image'] = ATTACH_POINTPROD.DS.$v['pgoods_image'].'_small.'.get_image_type($v['pgoods_image']);
                $pointprod_model = new pointprodModel ();
                $v['ex_state'] = $pointprod_model->getPointProdExstate($v);
                $list[$k] = $v;
            }
        }
        return $list;
    }
}